package me.sdevil507.org.po;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import me.sdevil507.supports.jpa.BaseJpaSnowflakeIdAndAuditEntity;
import org.springframework.boot.context.properties.bind.DefaultValue;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 机构用户信息实体类
 *
 * @author sdevil507
 */
@EqualsAndHashCode(callSuper = true, exclude = {"orgUserRel", "roles", "depts"})
@ToString(exclude = {"orgUserRel", "roles", "depts"})
@Entity
@Table(name = "user")
@Data
public class OrgUserPo extends BaseJpaSnowflakeIdAndAuditEntity {

    /**
     * 手机号码
     */
    @Column(name="phone_num")
    private String phoneNumber;

    /**
     * 密码
     */
    @Column(nullable = false, length = 40)
    private String password;

    @Column
    private String salt = "fzCE";

    /**
     * 真实姓名
     */
    @Column(name="realname")
    private String realName;

    /**
     * 个人头像
     */
    @Column
    private String profilePhoto;
    @Column
    private String avatar = "";

    /**
     * 邮箱
     */
    @Column(name="email")
    private String mail;

    /**
     * 身份证号码
     */
    @Column
    private String idNum;

    /**
     * 工号
     */
    @Column
    private String workNum;

    /**
     * 所属单位
     */
    @Column
    private String unit;

    /**
     * 职务
     */
    @Column
    private String jobTitle;

    /**
     * 备注
     */
    @Column
    private String summary;

    /**
     * 是否被锁定
     */
    @Column(nullable = false)
    private Boolean locked = false;

    /**
     * 最后登录时间
     */
    @Column
    private LocalDateTime lastLoginTime = LocalDateTime.now();

    /**
     * 密码修改时间
     */
    @Column
    private LocalDateTime passwordUpdateTime  = LocalDateTime.now();
    @Column
    private LocalDateTime passwordLastChanged  = LocalDateTime.now();

    /**
     * 逻辑删除标记
     */
    @Column(nullable = false)
    private boolean deleted = false;

    @Column
    private String lastLoginIp = "";
    @Column
    private Integer gender = 0;
    @Column
    private String remark = "";
    @Column
    private Integer status = 0;
    @Column
    private Integer userrole;
    @Column
    private String unionid;
    @Column
    private String openid;
    @Column
    private Integer isinit;
    @Column
    private Integer tcount;
    @Column
    private Integer usertype;
    @Column
    private Integer usertabtype;
    @Column
    private String company;
    @Column
    private Integer postId;
    @Column
    private Integer industryId;
    @Column
    private Integer provinceId;
    @Column
    private Integer authLater;
    @Column
    private String contactNum;
    /**
     * 0=不能邀请、1=VIP邀请（受邀人可继续邀请）、2=普通邀请（受邀人不能继续邀请）
     */
    @Column
    private Integer canInvite = 0;
    @Column
    private String moaenterid;
    @Column
    private Integer statsort;
    @Column
    private Integer researcher = 0;
    /**
     * 人员
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade= CascadeType.MERGE)
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Set<OrgUserRelPo> orgUserRel;

    /**
     * 包含角色
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "biz_org_user_role_rel",
            joinColumns = {
                    @JoinColumn(name = "user_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))},
            inverseJoinColumns = {
                    @JoinColumn(name = "role_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))})
    private Set<OrgRolePo> roles;

    /**
     * 所属部门
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade= CascadeType.MERGE)
    @JoinTable(name = "biz_org_user_dept_rel",
            joinColumns = {
                    @JoinColumn(name = "user_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))},
            inverseJoinColumns = {
                    @JoinColumn(name = "dept_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))})
    private List<OrgDeptPo> depts;

    /**
     * 如果是专家,则包含专家信息字段
     */
    @OneToOne(fetch = FetchType.LAZY, cascade= CascadeType.MERGE)
    @JoinColumn(name = "expert_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    private OrgExpertInfoPo expertInfo;

}
